#include "ringqueue.hpp"
#include "Task.hpp"
#include <iostream>
#include <unistd.h>

void *Consumer(void* args)
{
    ringqueue<Task> *rq = static_cast<ringqueue<Task> *>(args);
    while(true)
    {
        Task t;
        // 1. 消费
        rq->Pop(&t);

        // 2. 处理数据
        t();
        std::cout << "Consumer-> " << t.result() << std::endl;
    }
}
void *Productor(void* args)
{
    ringqueue<Task> *rq = static_cast<ringqueue<Task> *>(args);

    while(true)
    {
        sleep(1);

        // 1. 构造数据
        int x = rand() % 10 + 1; //[1, 10]
        usleep(x*1000);
        int y = rand() % 10 + 1;
        Task t(x, y);

        // 2. 生产
        rq->Push(t);

        std::cout << "Productor -> " << t.debug() << std::endl;
    }
}

int main()
{
    ringqueue<int> *bq = new ringqueue<int>();
    pthread_t c,p1,p2,p3,p4;
    pthread_create(&c, nullptr, Consumer, bq);
    pthread_create(&p1, nullptr, Productor, bq);
    pthread_create(&p2, nullptr, Productor, bq);
    pthread_create(&p3, nullptr, Productor, bq);
    pthread_create(&p4, nullptr, Productor, bq);

    pthread_join(c, nullptr);
    pthread_join(p1, nullptr);
    pthread_join(p2, nullptr);
    pthread_join(p3, nullptr);
    pthread_join(p4, nullptr);

    return 0;
}